//
//  QZXianyouVC.m
//  map
//
//  Created by 000 on 16/11/29.
//  Copyright © 2016年 faner. All rights reserved.
//

#import "QZXianyouVC.h"
#import "BmobSDK/Bmob.h"
#import "MJRefresh.h"
#import "UIView+Extestion.h"
#import "QZPopView.h"
@interface QZXianyouVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
    NSUserDefaults *userdefault1;
    NSMutableArray *deskMArray;
    NSMutableArray *currentProgress;
    NSMutableArray *thirdCountArray;
    NSMutableArray *whitchGroupArray;
    int arrayCount;
    int whitchGroup;
}
@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation QZXianyouVC

static NSString *cellIdentifier = @"cellIdentifier";

- (UICollectionView *)collectionView
{
    if (_collectionView == nil) {
        _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
        _collectionView.contentInset = UIEdgeInsetsMake(74, 0, 0, 0);
        _collectionView.showsVerticalScrollIndicator = NO;
        //注册collectionView的cell类型
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellIdentifier];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
    }
    return _collectionView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    userdefault1 = [NSUserDefaults standardUserDefaults];
    
    
    
    [self.view addSubview:self.collectionView];
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //加载个人任务图片
        [self loadthirdCount];
    }];
    [self.collectionView.mj_header beginRefreshing];
    self.collectionView.mj_header.automaticallyChangeAlpha = YES;
    

    
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    int currentCount = [thirdCountArray.lastObject intValue];
    
    return currentCount;
//    return deskMArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:deskMArray[indexPath.row]]];
    if (cell.contentView.subviews.count ==0) {
        [cell.contentView addSubview:imageView];
    }
    return cell;
}
#pragma mark - UICollectionViewDelegateFlowLayout
//设置每一item的size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(280/3.0, 280/3.0);
}
- (void)loadPresonPic//第一步加载任务图片
{
    BmobQuery *query = [BmobQuery queryWithClassName:@"personDesk"];
    [query orderByAscending:@"createdAt"];
    [query whereKey:@"loginUser" equalTo:[userdefault1 objectForKey:@"detailSave"]];
    [query calcInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        deskMArray = [NSMutableArray array];
        int f = 0;
        for (BmobObject *obj in array) {
            f++;
            NSString *breakPoints = [obj objectForKey:@"deskImStr"];
            [deskMArray addObject:breakPoints];
            if (f==array.count) {
                [self.collectionView reloadData];
                [self.collectionView.mj_header endRefreshing];
            }
        }
        [self.collectionView.mj_header endRefreshing];
    }];
}
- (void)loadthirdCount//第一步加载thirdCount
{
    BmobQuery *query = [BmobQuery queryWithClassName:@"thirdCount"];
    [query orderByAscending:@"createdAt"];
    [query whereKey:@"loginUser" equalTo:[userdefault1 objectForKey:@"detailSave"]];
    [query calcInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        thirdCountArray = [NSMutableArray array];
        int f = 0;
        for (BmobObject *obj in array) {
            f++;
            NSString *breakPoints = [obj objectForKey:@"thirdC"];
            [thirdCountArray addObject:breakPoints];
            if (f == array.count) {
                [self loadPresonPic];
            }
        }
    }];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    QZPopView *popView = [QZPopView popViewWithFrame:self.view.bounds];
    popView.imageView.image = [UIImage imageNamed:deskMArray[indexPath.row]];
    [popView show];
}

@end
